//
//  ViewController.m
//  JSAndOC
//
//  Created by 红沙尘 on 2018/11/16.
//  Copyright © 2018年 红沙尘. All rights reserved.
//

#import "ViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>

@interface ViewController ()<UIWebViewDelegate> {
    
    __weak IBOutlet UIWebView *wb;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];

    NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    NSString *basePath = [[NSBundle mainBundle] bundlePath];

    NSURL *baseURL = [NSURL fileURLWithPath:basePath];

    [wb loadHTMLString:htmlString baseURL:baseURL];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
    JSContext *context = [wb valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    context[@"ocfun1"] = ^() {
        NSLog(@"ocfun1");
    };
    
    context[@"ocfun2"] = ^() {
        NSLog(@"js给oc传字符串");
        NSArray *args = [JSContext currentArguments];
        for (NSString *str in args) {
            NSLog(@"js给oc传字符串 = %@",str);
        }
    };
    
    context[@"ocfun3"] = ^() {
        NSLog(@"js给oc传数组");
        NSArray *args = [JSContext currentArguments];
        for (NSString *str in args) {
            NSLog(@"js给oc传数组 = %@",str);
        }
    };
    
    context[@"testFunc"] = ^() {
        NSLog(@"js给oc传字典");
        
        JSValue *value = [JSContext currentThis];
        NSDictionary *dict = value.toDictionary;
        NSLog(@"%@",dict);
    };
}

- (IBAction)js1 {
    JSContext *ct = [wb valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    NSString *js = [NSString stringWithFormat:@"js1()"];
    [ct evaluateScript:js];
}

- (IBAction)js2 {
    JSContext *ct = [wb valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    NSString *js = [NSString stringWithFormat:@"js2('%@')",@"字符串"];
    [ct evaluateScript:js];
}

- (IBAction)js3 {
    
    NSDictionary *dict = @{@"name":@"小明"};
    
    JSContext *ct = [wb valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    NSString *js = [NSString stringWithFormat:@"js3(%@)",[self dictToString:dict]];
    [ct evaluateScript:js];
}

- (NSString *)dictToString:(NSDictionary *)object {
    NSString *jsonString = nil;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];
    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}

- (IBAction)js4 {
    NSArray *array = @[@"第一个",@"第二个",@"第三个"];
    JSContext *ct = [wb valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    JSValue *v = ct[@"js4"];
    [v callWithArguments:array];
}


@end
